package com.ruoyi.Processor;

import com.ruoyi.common.entity.Project;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

// HTML解析服务
public class HtmlParserService {
    private ProcessorRegistry registry;
    
    public HtmlParserService() {
        registry = new ProcessorRegistry();
        // 注册处理器
        registry.registerProcessor(new TitleProcessor());
        registry.registerProcessor(new ParagraphProcessor());
        registry.registerProcessor(new ListProcessor());
    }
    
    public Project parseHtmlToProject(String html) {
        Document doc = Jsoup.parse(html);
        Project project = new Project();
        
        // 遍历所有元素并进行处理
        processElement(doc.body(), project);
        
        return project;
    }
    
    private void processElement(Element element, Project project) {
        // 处理当前元素
        registry.processElement(element, project);
        
        // 递归处理子元素
        for (Element child : element.children()) {
            processElement(child, project);
        }
    }
    
    // 可以添加更多注册处理器的方法
    public void registerCustomProcessor(ContentProcessor processor) {
        registry.registerProcessor(processor);
    }
}